package com.hnucm.c202001020214;

import androidx.appcompat.app.AppCompatActivity;
import androidx.room.Room;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.gyf.immersionbar.ImmersionBar;

public class Enroll extends BaseActivity {

    private Button button;
    EditText e1,e2,e3;
    UserDaoDatabase userDaoDatabase;
    UserDao userDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_enroll);
        userDaoDatabase = Room.databaseBuilder(this,UserDaoDatabase.class,"Mytest")
                .allowMainThreadQueries()//允许在主线程中调用
                .build();
        //返回接口的实现类（动态生成的类）
        userDao = userDaoDatabase.getUserDao();


        ImmersionBar.with(this)
                // .statusBarColor(R.color.touming)
                .init();
        e3=findViewById(R.id.username);
        e1=findViewById(R.id.password);
        e2=findViewById(R.id.agpassword);
        button = findViewById(R.id.enroll);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(e2.getText().toString().equals(e1.getText().toString())){
                    User user1 = new User();
                    user1.telphone =e3.getText().toString();
                    user1.password =e2.getText().toString();
                    userDao.insertUser(user1);
                    Toast.makeText(Enroll.this,"注册成功",Toast.LENGTH_LONG).show();

                    Intent intent=new Intent(Enroll.this,MainActivity.class);

                    //关闭前面所有Activity
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                    finish();
                }
                else{
                    Toast.makeText(Enroll.this,"两次输入的密码不一致",Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}